package ru.kest.babymonitor.menu;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.*;
import android.widget.*;
import ru.kest.babymonitor.R;
import ru.kest.babymonitor.Settings;

/**
 * Class Created by Konstantin Kharitonov on 31.07.2014.
 */
public class MenuPanel {

    private static String TAG = "IPCamBabyMonitor";

    PopupWindow popupWindow = null;

    Context context = null;
    LayoutInflater layoutInflater = null;
    View.OnClickListener onClickListener = null;
    View menuView = null;

    public MenuPanel(Context context, View.OnClickListener onClickListener, LayoutInflater lo) {
        this.context = context;
        this.onClickListener = onClickListener;
        this.layoutInflater = lo;

        menuView = layoutInflater.inflate(R.layout.menu_panel, null);
        ViewGroup menuContainer = (ViewGroup) menuView.findViewById(R.id.menu_container);
        for(int i=0; i < menuContainer.getChildCount(); ++i) {
            View nextChild = menuContainer.getChildAt(i);
            nextChild.setOnClickListener(onClickListener);
        }
        View v = menuContainer.findViewById(R.id.soundButton);
        v.setSelected(Settings.getInstance().isAudioOn());
        v = menuContainer.findViewById(R.id.alarmButton);
        v.setSelected(Settings.getInstance().isAlarmOn());
    }

    public synchronized void show(View v) {
        Log.i(TAG, "Menu panel showing");
        if (popupWindow != null) return; //already showing
        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        Point displaySize = new Point();
        display.getSize(displaySize);

        popupWindow = new PopupWindow(menuView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, false);
        popupWindow.setAnimationStyle(android.R.style.Animation_InputMethod);
        popupWindow.setWidth(displaySize.x);
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    /**
     * Hide your menu.
     */
    public synchronized void hide() {
        if (popupWindow != null) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

    public boolean isShowing() {
        return popupWindow != null;
    }

    public View getMenuView() {
        return menuView;
    }
}
